/**
 * Title:       MessageHelper.java
 * Copyright:   Copyright (c) 1996-2004 The Agent Factory Working Group. All rights reserved.
 * Licence:     This file is free software; you can redistribute it and/or modify
 *              it under the terms of the GNU Lesser General Public License as published by
 *              the Free Software Foundation; either version 2.1, or (at your option)
 *              any later version.
 *
 *              This file is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU Lesser General Public License for more details.
 *
 *              You should have received a copy of the GNU Lesser General Public License
 *              along with Agent Factory; see the file COPYING.  If not, write to
 *              the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *              Boston, MA 02111-1307, USA.
 */
package com.agentfactory.platform.mts;

import com.agentfactory.platform.mts.FIPAContent;
import java.util.ArrayList;

/**
 * This class implements methods that support the FIPAMessageModule.
 *
 * @author  Rem Collier
 */
public class MessageHelper {
    public static String toBelief(Message message) {
        StringBuffer buf = new StringBuffer();
        buf.
            append("BELIEF(fipaMessage(").
            append(message.getPerformative()).
            append(",sender(").
            append(message.getSender().getName()).
            append(",addresses(");
        ArrayList addresses = message.getSender().getAddresses();
        if (addresses.size() > 0) {
            buf.append(addresses.get(0));
        }
        for (int i = 1; i < addresses.size(); i++) {
            buf.append(",").append(addresses.get(i));
        }
        buf.
            append(")),").
            append(message.getContent()).
            append("))");
        return buf.toString();
    }
    
    public static String convertFromFIPASL0(FIPAContent content) {
        StringBuffer buf = new StringBuffer();
        String arg = null;
        buf.append(content.getCommand());
        if (content.size() > 0) {
            buf.append("(");
            boolean first = true;
            for (int i=0; i < content.size(); i++) {
                if (first) {
                    first = false;
                } else {
                    buf.append(",");
                }
                arg = convertFromFIPASL0(content.get(i));
                if (arg.startsWith(":")) {
                    arg = arg.substring(0, arg.length());
                    buf.append(arg).append("(");
                    buf.append(convertFromFIPASL0(content.get(++i)));
                    buf.append(")");
                } else {
                    buf.append(convertFromFIPASL0(content.get(i)));
                }
            }
            buf.append(")");
        }
        return buf.toString();
    }

}
